import 'package:flutter/painting.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hx_im_plugin/hx_im_plugin.dart';
import 'package:hx_im_plugin_example/hq_im_ui/utils/envent_bus.dart';

class IMHelper {

  /// 插入一条自定义消息
  static Future<void> insertCustomMessage(EMConversation conversation) async {

    /// 创建自定义消息
    Map<String, String> params = {
      "id": "YD74636363545454",
      "status": "0",
      "startCity": "榆林市",
      "endCity": "成都市",
      "distance": "1450km",
      "name": "煤炭",
      "price": "200元/吨",
      "time": "2021.05.07 14:35",
      "driver": "张三",
      "phone": "18788888888",
      "carNumber": "陕A76182"
    };
    EMMessage msg = EMMessage.createCustomSendMessage(
        conversation.id,
        'ORDER',
        params: params);

    try {
      /// 向会话中插入一条消息
      await conversation.insertMessage(msg);
    } on EMError catch (e) {
      print('操作失败，原因是: $e');
    }
  }

  /// 更新消息
  static Future<void> updateCustomMessage(EMConversation conversation) async {

    /// 创建自定义消息
    Map<String, String> params = {
      "id": "YD74636363545454",
      "status": "1",
      "startCity": "榆林市",
      "endCity": "成都市",
      "distance": "1450km",
      "name": "煤炭",
      "price": "200元/吨",
      "time": "2021.05.07 14:35",
      "driver": "张三",
      "phone": "18788888888",
      "carNumber": "陕A76182"
    };
    EMMessage msg;
    List<EMMessage> msgs = await conversation.loadMessages();
    for(int i = 0; i < msgs.length; i++) {
      if(msgs[i].body.type == EMMessageBodyType.CUSTOM) {
        EMCustomMessageBody body = msgs[i].body;
        body.params = params;
        msg = msgs[i];
      }
    }

    if (msg != null) {
      try {
        bool isUpdate = await conversation.updateMessage(msg);
        if(isUpdate) {
          bus.emit("updateCustomMessage", true);
        }
      } on EMError catch (e) {
        print('操作失败，原因是: $e');
      }
    }

  }

  /// 消息转发
  static Future<void> forwardMessage(EMMessage msg, List<String> users) async {

    EMMessage message;
    switch(msg.body.type) {
      case EMMessageBodyType.TXT:
        /// 文本消息
        EMTextMessageBody body = msg.body;
        users.forEach((element) async {
          message = EMMessage.createTxtSendMessage(element, content: body.content);
          if(message != null) {
            try {
              EMMessage forwardMsg = await EMClient.getInstance.chatManager.sendMessage(message);
              if (forwardMsg != null) {
                SmartDialog.showToast('转发成功', alignment: Alignment.center);
              }
            } on EMError catch(e) {
              print('操作失败，原因是: $e');
            }
          }
        });
        break;
      case EMMessageBodyType.IMAGE:
        /// 图片消息
        EMImageMessageBody body = msg.body;
        users.forEach((element) async {
          message = EMMessage.createImageSendMessage(element, body.localPath);
          if(message != null) {
            try {
              EMMessage forwardMsg = await EMClient.getInstance.chatManager.sendMessage(message);
              if (forwardMsg != null) {
                SmartDialog.showToast('转发成功', alignment: Alignment.center);
              }
            } on EMError catch(e) {
              print('操作失败，原因是: $e');
            }
          }
        });
        break;
      case EMMessageBodyType.CUSTOM:
        /// 自定义消息
        EMCustomMessageBody body = msg.body;
        users.forEach((element) async {
          message = EMMessage.createCustomSendMessage(element, 'ORDER', params: body.params);
          if(message != null) {
            try {
              EMMessage forwardMsg = await EMClient.getInstance.chatManager.sendMessage(message);
              if (forwardMsg != null) {
                SmartDialog.showToast('转发成功', alignment: Alignment.center);
              }
            } on EMError catch(e) {
              print('操作失败，原因是: $e');
            }
          }
        });
        break;
    }

  }


}